<?php
/**
 * Author : CuongNV
 */
//App::uses('Sanitize', 'Utility');
class StoreysController extends AppController {
	public function beforeFilter() {
	    parent::beforeFilter();
	}
	public function index() {
		$this->Storey->recursive = 0;
		$this->set('storeys', $this->Paginator->paginate());
	}

	public function view($id = null) {
		if (!$this->Storey->exists($id)) {
			throw new NotFoundException(__('Invalid storey'));
		}
		$options = array('conditions' => array('Storey.' . $this->Storey->primaryKey => $id));
		$this->set('storey', $this->Storey->find('first', $options));
	}

	public function get_first(){
		$this->autoRender = false;
		$data = json_decode($_GET["id"]);
		$result = $this->Storey->find('first',array(
			'conditions' => array('Storey.id' => $data->key),
			'fields' => array('Storey.type')
		));
		echo json_encode($result);
	}

	public function add() {
		if ($this->request->is('post')) {
			$this->Storey->create();
			$this->request->data['Storey']['order'] = 1;
			$this->request->data['Storey']['type'] = strip_tags($this->request->data['Storey']['type']);
			if ($this->Storey->save($this->request->data)) {
				return $this->redirect(array('controller'=>'rooms','action' => 'index'));
			}
		}
	}
	public function edit($id = null) {
		if (!$this->Storey->exists($id)) {
			return $this->redirect(array('controller' => 'rooms', 'action' => 'index'));
		}
		if ($this->request->is(array('post', 'put'))) {
			$this->request->data['Storey']['id'] = $id;
			$this->request->data['Storey']['type'] = strip_tags($this->request->data['Storey']['type']);
			$this->Storey->save($this->request->data);
			return $this->redirect(array('controller' => 'rooms', 'action' => 'index'));
		} else {
			$options = array('conditions' => array('Storey.' . $this->Storey->primaryKey => $id));
			$this->request->data = $this->Storey->find('first', $options);
		}
	}


	public function delete($id = null) {
		$this->Storey->id = $id;
		if (!$this->Storey->exists()) {
			return $this->redirect(array('controller'=>'rooms','action' => 'index'));
		}
		$this->loadModel('Room');
		$check = $this->Room->find('first', array('conditions' => array('Room.storey_id' => $id)));
		if($check){
			$link = "http://".$_SERVER["HTTP_HOST"].$this->webroot."rooms";
			echo '<script>';
			echo 'alert("Lầu đang tồn tại phòng không thể xóa !");';
			echo "window.location.href = '<?php echo $link; ?>' ";
			echo '</script>';
		}else{
			$this->Storey->delete();
			return $this->redirect(array('controller'=>'rooms','action' => 'index'));
		}
	}

	public function admin_index() {
		$this->Storey->recursive = 0;
		$this->set('storeys', $this->Paginator->paginate());
	}

	public function admin_view($id = null) {
		if (!$this->Storey->exists($id)) {
			throw new NotFoundException(__('Invalid storey'));
		}
		$options = array('conditions' => array('Storey.' . $this->Storey->primaryKey => $id));
		$this->set('storey', $this->Storey->find('first', $options));
	}

	public function admin_add() {
		if ($this->request->is('post')) {
			$this->Storey->create();
			if ($this->Storey->save($this->request->data)) {
				$this->Session->setFlash(__('The storey has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The storey could not be saved. Please, try again.'));
			}
		}
	}

	public function admin_edit($id = null) {
		if (!$this->Storey->exists($id)) {
			throw new NotFoundException(__('Invalid storey'));
		}
		if ($this->request->is(array('post', 'put'))) {
			if ($this->Storey->save($this->request->data)) {
				$this->Session->setFlash(__('The storey has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The storey could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Storey.' . $this->Storey->primaryKey => $id));
			$this->request->data = $this->Storey->find('first', $options);
		}
	}

	public function admin_delete($id = null) {
		$this->Storey->id = $id;
		if (!$this->Storey->exists()) {
			throw new NotFoundException(__('Invalid storey'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Storey->delete()) {
			$this->Session->setFlash(__('The storey has been deleted.'));
		} else {
			$this->Session->setFlash(__('The storey could not be deleted. Please, try again.'));
		}
		return $this->redirect(array('action' => 'index'));
	}


}
